package com.cysyz.mylib.utils.xsvmaker.impl;


import com.cysyz.mylib.utils.myassert.MyAssert;
import com.cysyz.mylib.utils.xsvmaker.XsvMaker;

import java.util.Collection;

/**
 * 生成各种特殊符号分割的字符串工具
 */
public class XsvMakerImpl implements XsvMaker {
    private StringBuilder stringBuilder = new StringBuilder();
    private String separator;

    public XsvMakerImpl(String separator) {
        this.separator = separator;
    }

    public XsvMakerImpl() {
        this.separator = ",";
    }
    /**
     *@Title: append
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014年6月10日 下午5:11:34
     * @param value
     */
    @Override
    public XsvMaker append(String value) {
        if (!MyAssert.isNullOrEmpty(value)) {
            addSomethingPreSeparator(stringBuilder);
            stringBuilder.append(value).append(separator);
        }
        return this;
    }

    @Override
    public <T>XsvMaker collectionToXsv(Class<T> clazz, Collection<T> collection, CollectionValueGetter<T> collectionValueGetter) {
        MyAssert.notNullMulti(clazz, collection, collectionValueGetter);
        for (T element : collection) {
            this.append(collectionValueGetter.getValue(element));
        }
        return this;
    }

    protected void addSomethingPreSeparator(StringBuilder stringBuilder2) {}

    /**
     *@Title: getResult
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014年6月10日 下午5:11:34
     * @return
     */
    @Override
    public String getResult() {
        if (stringBuilder.length() != 0) {
            return stringBuilder.substring(0, stringBuilder.length() -1);
        }
        return stringBuilder.toString();
    }


    @Override
    public String toString() {
        return this.getResult();
    }

    public static void main(String[] args) {
        XsvMaker xsvMaker = new XsvMakerImpl("-");
        xsvMaker.append("wwww").append("xxxx").append("gggggwdsf");
        System.out.println(xsvMaker);
    }
}
